/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package com.redxun.config;

import com.redxun.core.swagger.SwaggerProperties;
import com.redxun.core.swagger.SwaggerUtil;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Swagger配置类.
 * @author aj
 *
 */
@Configuration
@EnableSwagger2
@AllArgsConstructor
public class SwaggerConfiguration {

    private final SwaggerProperties swaggerProperties;

    @Bean
    public Docket orgDocket() {
        return docket("组织架构", Collections.singletonList("com.redxun.user.org.controller"));
    }

    @Bean
    public Docket authDocket() {
        return docket("授权模块", Collections.singletonList("com.redxun.auth"));
    }

    @Bean
    public Docket sysDocket() {
        return docket("系统模块",    Collections.singletonList("com.redxun.system.core.controller"));
    }

    @Bean
    public Docket formDocket() {
        return docket("单据模块", Collections.singletonList("com.redxun.form.core.controller"));
    }
	
		@Bean
		public Docket bpmDocket() {
			return docket("工作流模块", Collections.singletonList("com.redxun.bpm.core.controller"));
		}

    private Docket docket(String groupName, List<String> basePackages) {
        ParameterBuilder ticketPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        ticketPar.name("Authorization").description("User Authorization")
                .modelRef(new ModelRef("string")).parameterType("header")
						    //header中的ticket参数非必填，传空也可以
                .required(true).build();
				//根据每个方法名也知道当前方法在设置什么参数
        pars.add(ticketPar.build());

        return new Docket(DocumentationType.SWAGGER_2)
            .groupName(groupName)
            .apiInfo(apiInfo())
            .select()
            .apis(SwaggerUtil.basePackages(basePackages))
            //扫描指定包中的swagger注解
            //扫描所有有注解的ApiOperation，用这种方式更灵活
            //.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
            .paths(PathSelectors.any())
            .build()
            .globalOperationParameters(pars);
						//.securitySchemes(Lists.<SecurityScheme>newArrayList(SwaggerUtil.clientInfo(), SwaggerUtil.auth(), SwaggerUtil.tenant()));
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            .title(swaggerProperties.getTitle())
            .description(swaggerProperties.getDescription())
            .license(swaggerProperties.getLicense())
            .licenseUrl(swaggerProperties.getLicenseUrl())
            .termsOfServiceUrl(swaggerProperties.getTermsOfServiceUrl())
            .contact(new Contact(swaggerProperties.getContact().getName(), swaggerProperties.getContact().getUrl(), swaggerProperties.getContact().getEmail()))
            .version(swaggerProperties.getVersion())
            .build();
    }

}
